<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="项目" prop="projectId">
        <el-select
          style="width:100%"
          v-model="dataForm.projectId"
          placeholder="请选择项目"
        >
          <el-option
            v-for="item in optionsprojectId"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select
          style="width:100%"
          v-model="dataForm.gender"
          placeholder="请选择性别"
        >
          <el-option
            v-for="item in optionsGender"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="电话号码" prop="mobilePhone">
        <el-input
          v-model="dataForm.mobilePhone"
          placeholder="电话号码"
        ></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="idNumber">
        <el-input v-model="dataForm.idNumber" placeholder="身份证号"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
      </el-form-item>
      <el-form-item label="居住地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="居住地址"></el-input>
      </el-form-item>
      <el-form-item label="民族" prop="nationality">
        <el-input v-model="dataForm.nationality" placeholder="民族"></el-input>
      </el-form-item>

      <el-form-item label="发证机关" prop="issueorgan">
        <el-input
          v-model="dataForm.issueorgan"
          placeholder="发证机关"
        ></el-input>
      </el-form-item>
      <el-form-item label="文化程度" prop="education">
        <el-input
          v-model="dataForm.education"
          placeholder="文化程度"
        ></el-input>
      </el-form-item>
      <el-form-item label="入册工龄" prop="workYear">
        <el-input v-model="dataForm.workYear" placeholder="入册工龄"></el-input>
      </el-form-item>
      <el-form-item label="出生日期" prop="birthdate">
        <el-date-picker
          style="width:100%"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          v-model="dataForm.birthdate"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="开始工作日期" prop="startWorkDate">
        <el-date-picker
          style="width:100%"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          v-model="dataForm.startWorkDate"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="职位" prop="positionTitle">
        <el-select
          style="width:100%"
          v-model="dataForm.positionTitle"
          placeholder="请选择职位"
        >
          <el-option
            v-for="item in optionsPositionTitle"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="人员类型" prop="personnelType">
        <el-select
          style="width:100%"
          v-model="dataForm.personnelType"
          placeholder="请选择人员类型"
        >
          <el-option
            v-for="item in optionspersonnelType"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单位类型" prop="unitType">
        <el-select
          style="width:100%"
          v-model="dataForm.unitType"
          placeholder="请选择单位类型"
        >
          <el-option
            v-for="item in optionSunitType"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isIdCard, isMobile } from "@/utils/validate";
export default {
  data() {
    var validateisIdCard = (rule, value, callback) => {
      if (!isIdCard(value)) {
        callback(new Error("身份证号码格式错误"));
      } else {
        callback();
      }
    };
    var validateisMobile = (rule, value, callback) => {
      if (!isMobile(value)) {
        callback(new Error("电话号码格式错误"));
      } else {
        callback();
      }
    };

    return {
      projectId: "",
      optionsprojectId: [],
      optionsGender: [],
      optionsPositionTitle: [],
      optionspersonnelType: [],
      optionSunitType: [],
      visible: false,
      dataForm: {
        id: 0,
        projectId: "",
        name: "",
        gender: "",
        mobilePhone: "",
        idNumber: "",
        age: "",
        positionTitle: "",
        personnelType: "",
        unitType: "",
        projectCode: "",
        birthdate: "",
        address: "",
        nationality: "",
        issueorgan: "",
        education: "",
        startWorkDate: "",
        workYear: ""
      },
      dataRule: {
        projectId: [
          { required: true, message: "项目不能为空", trigger: "blur" }
        ],
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        gender: [{ required: true, message: "性别不能为空", trigger: "blur" }],
        mobilePhone: [
          { required: true, message: "电话号码不能为空", trigger: "blur" },
          { validator: validateisMobile, trigger: "blur" }
        ],
        idNumber: [
          { required: true, message: "身份证号不能为空", trigger: "blur" },
          { validator: validateisIdCard, trigger: "blur" }
        ],
        age: [
          { required: true, message: "年龄不能为空", trigger: "blur" },
          { pattern: /^\+?[1-9]\d*$/, message: "请输入大于0的正整数" }
        ],
        positionTitle: [
          { required: true, message: "职位不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 获取项目和性别
    getCompanyTypes() {
      this.$http({
        url: this.$http.adornUrl(`/sys/getParamenters/getProjectList`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionsprojectId = data.list;
        }
      });
      //获取性别
      this.$http({
        url: this.$http.adornUrl(`/sys/getParamenters/getGender`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionsGender = data.list;
        }
      });
      this.$http({
        url: this.$http.adornUrl(`/sys/getParamenters/getPositionTitle`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionsPositionTitle = data.list;
        }
      });
      this.$http({
        url: this.$http.adornUrl(`/sys/getParamenters/getPersonnelType`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionspersonnelType = data.list;
        }
      });
      this.$http({
        url: this.$http.adornUrl(`/sys/getParamenters/getUnitType`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionSunitType = data.list;
        }
      });
    },
    init(id) {
      this.getCompanyTypes();
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/bpersonnelprofile/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.projectId = data.bPersonnelProfile.projectId;
              this.dataForm.name = data.bPersonnelProfile.name;
              this.dataForm.gender = data.bPersonnelProfile.gender;
              this.dataForm.mobilePhone = data.bPersonnelProfile.mobilePhone;
              this.dataForm.idNumber = data.bPersonnelProfile.idNumber;
              this.dataForm.age = data.bPersonnelProfile.age;
              this.dataForm.positionTitle =
                data.bPersonnelProfile.positionTitle;
              this.dataForm.personnelType =
                data.bPersonnelProfile.personnelType;
              this.dataForm.unitType = data.bPersonnelProfile.unitType;

              this.dataForm.birthdate = data.bPersonnelProfile.birthdate;
              this.dataForm.address = data.bPersonnelProfile.address;
              this.dataForm.nationality = data.bPersonnelProfile.nationality;
              this.dataForm.issueorgan = data.bPersonnelProfile.issueorgan;
              this.dataForm.education = data.bPersonnelProfile.education;
              this.dataForm.startWorkDate =
                data.bPersonnelProfile.startWorkDate;
              this.dataForm.workYear = data.bPersonnelProfile.workYear;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/bpersonnelprofile/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              projectId: this.dataForm.projectId,
              name: this.dataForm.name,
              gender: this.dataForm.gender,
              mobilePhone: this.dataForm.mobilePhone,
              idNumber: this.dataForm.idNumber,
              age: this.dataForm.age,
              positionTitle: this.dataForm.positionTitle,
              personnelType: this.dataForm.personnelType,
              unitType: this.dataForm.unitType,
              birthdate: this.dataForm.birthdate,
              address: this.dataForm.address,
              nationality: this.dataForm.nationality,
              issueorgan: this.dataForm.issueorgan,
              education: this.dataForm.education,
              startWorkDate: this.dataForm.startWorkDate,
              workYear: this.dataForm.workYear
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
